<?php
define('APPPATH', dirname(__FILE__).'/../');

class APP {
	public static $instance;
	public static $CONFIG = array();
	public static $V = array();

	public function __get($property) {
		return self::$instance[$property];
	}

	public function __set($property, $value) {
		return self::$instance[$property] = $value;
	}

	public function load($file, $class=null) {
		$APP = new APP();
		if ($class && $APP->$class) {
			return $APP->$class;
		}
		if (!file_exists($file)) {
			throw new Exception("load_file_does_not_exists $file");
		}
		require_once $file;
		if ($class && class_exists($class)) {
			$APP->$class = new $class();
			return $APP->$class;
		}
	}

	public function load_config($name) {
		self::load(APPPATH."config/{$name}.php");
		return APP::$CONFIG[$name];
	}

	public function load_helper($name) {
		self::load(APPPATH."helper/{$name}.helper.php");
	}
}

function load($file, $class=null) {
	$APP = new APP();
	if ($class && $APP->$class) {
		return $APP->$class;
	}
	if (!file_exists($file)) {
		throw new Exception("load_file_does_not_exists $file");
	}
	require_once $file;
	if ($class && class_exists($class)) {
		$APP->$class = new $class();
		return $APP->$class;
	}
}

function load_core($name) {
	require_once APPPATH.'core/'.$name.'.php';
}

function load_model($name) {
	return load(APPPATH."model/{$name}.model.php", ucfirst($name.'_model'));
}

function load_library($name) {
	return load(APPPATH."library/{$name}.library.php", 
		ucfirst($name.'_library'));
}

function load_helper($name) {
	load(APPPATH."helper/{$name}.helper.php");
}

function load_view($name) {
	load(APPPATH."view/{$name}.view.php");
}

function load_config($name) {
	load(APPPATH."config/{$name}.php");
	return APP::$CONFIG[$name];
}